// API调试工具
import { apiClient } from '@/api/base'
import { apiConfig, isDevelopment, isProduction } from '@/config/api'

// API连接测试
export const testApiConnection = async () => {
  const results = {
    config: {
      baseURL: apiConfig.baseURL,
      timeout: apiConfig.timeout,
      environment: isDevelopment ? 'development' : isProduction ? 'production' : 'unknown'
    },
    tests: []
  }

  // 测试1: 检查配置
  try {
    results.tests.push({
      name: '配置检查',
      status: 'success',
      message: `基础URL: ${apiConfig.baseURL}, 超时: ${apiConfig.timeout}ms`
    })
  } catch (error) {
    results.tests.push({
      name: '配置检查',
      status: 'error',
      message: error.message
    })
  }

  // 测试2: 检查网络连接
  try {
    const response = await fetch('/api/health', {
      method: 'GET'
    })
    results.tests.push({
      name: '网络连接',
      status: response.ok ? 'success' : 'error',
      message: response.ok ? '连接成功' : `HTTP ${response.status}`
    })
  } catch (error) {
    results.tests.push({
      name: '网络连接',
      status: 'error',
      message: error.message
    })
  }

  // 测试3: 检查代理配置
  try {
    const response = await fetch('/api/auth/login', {
      method: 'OPTIONS'
    })
    results.tests.push({
      name: '代理配置',
      status: 'success',
      message: '代理配置正常'
    })
  } catch (error) {
    results.tests.push({
      name: '代理配置',
      status: 'error',
      message: error.message
    })
  }



  return results
}

// 测试登录API
export const testLoginApi = async (username = 'test', password = 'test') => {
  try {
    const response = await apiClient.post('/auth/login', {
      username,
      password
    })
    return {
      status: 'success',
      message: '登录API正常',
      data: response.data
    }
  } catch (error) {
    return {
      status: 'error',
      message: error.message || '登录API错误',
      error: error
    }
  }
}

// 获取API状态信息
export const getApiStatus = () => {
  return {
    baseURL: apiConfig.baseURL,
    timeout: apiConfig.timeout,
    environment: isDevelopment ? 'development' : isProduction ? 'production' : 'unknown',
    userAgent: navigator.userAgent,
    timestamp: new Date().toISOString()
  }
} 